﻿// var authority = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理
authority[2] = 1;
var deputeID = GetQueryString("id");//该函数在head.js中定义，用于获取url参数
//发送请求获取委托单基本信息
var sentMessage = {
    "requestType": "initBaseData",
    "task_id": deputeID
};
var postData;
$.ajax({
    url: '/task/checkDepute',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);
//显示基本信息
var basicInfomation = document.getElementById("infomation").children[0];
var basicInfomationData = [
    ["委托单位", "委托说明", "项目名称", "产品名称", "检测时间", "产品编号", "检测标准", "检测部位", "热处理状态", "检测地点"],
    []];//保存基本信息的数组，下标0是字段名，下标1是字段值。
basicInfomationData[1] = [//postData是前面ajax请求保存的数据，这里把它放进basicInfomationData里面，用于下面显示这些数据
    postData[0].task_company,
    postData[0].task_description,
    postData[0].task_pname,
    postData[0].task_product,
    postData[0].task_time,
    postData[0].task_pdid,
    postData[0].task_standard,
    postData[0].task_position,
    postData[0].task_heat,
    postData[0].task_addr
];
document.getElementById("head").innerHTML = postData[0].task_type + "委托单";//这里改了标题名称
for (let i = 0; i < basicInfomationData[0].length; i++) {
    let tr = document.createElement("tr");
    if (i % 3 === 0) {//一行显示三个元素，可以修改
        basicInfomation.appendChild(tr);
    }
    let td = document.createElement("td");
    td.setAttribute("class", "td-shadows");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[0][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);//加一格用于显示字段名
    td = document.createElement("td");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[1][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);//加一格用于显示字段值
}

//发送请求获取工件信息
sentMessage = {
    "requestType": "initWorkCountData",
    "task_id": deputeID
};
$.ajax({
    url: '/task/checkDepute',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);
//显示工件信息
var workpieceTable = document.getElementById("workpieceTable");
var workpieceInfomation = [];
for (let i = 0; i < postData.length; i++) {
    workpieceInfomation[i] = [//熟悉的操作
        postData[i].wc_id,
        postData[i].wc_pipeName,
        postData[i].wc_spec,
        postData[i].wc_texture,
        postData[i].wc_type,
        postData[i].wc_weldM,
        postData[i].tech_id,
        postData[i].tech_state,
        postData[i].team_name
    ];
    //若：没有工艺卡&&没有检测小组&&拥有权限2，则显示新建工艺卡按钮
    if (postData[i].tech_id == null && postData[i].team_name == null && authority[2] === 1) {
        workpieceInfomation[i][6] = "<button class=\"btn btn-xs btn-success\" onclick=\"window.location.href='/newTecCard.html?id=" + postData[i].wc_id + "'\">新建</button>";
    }
    //若：没有检测小组，则：没有工艺卡或者工艺卡已审核时显示新建检测小组，否则显示无
    if (postData[i].team_name == null) {
        if ((postData[i].tech_id == null || postData[i].tech_state == 1) && authority[2] === 1) {
            workpieceInfomation[i][8] = "<button class=\"btn btn-xs btn-success\" onclick=\"OnAllocate(" + postData[i].wc_id + ', ' + i + ");\">分配</button>";
        } else {
            workpieceInfomation[i][8] = "无";
        }        
    }
    switch (postData[i].tech_state) {//审核不通过时，显示修改工艺卡按钮
        case null: {
            workpieceInfomation[i][7] = "未分配";
            break;
        }
        case 0: {
            workpieceInfomation[i][7] = "审核中";
            break;
        }
        case 1: {
            workpieceInfomation[i][7] = "审核通过";
            break;
        }
        case 2: {
            workpieceInfomation[i][7] = "<button class=\"btn btn-xs btn-success\" onclick=\"window.location.href='/modifyTecCard.html?id=" + postData[i].tech_id + "'\">未通过，点击修改</button>";
            break;
        }
    }
}
//初始化工艺卡表格
for (let i = 0; i < workpieceInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < workpieceInfomation[0].length; j++) {
        inner += "<td>" + workpieceInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    workpieceTable.children[1].appendChild(tr);
}

var wc_id;
var teamName = [];
//分配检测小组的点击事件
function OnAllocate(id, index) {
    console.log(workpieceInfomation[index])
    if (workpieceInfomation[index][7] == '未分配') {
        if (confirm('当前工件未分配工艺卡，是否不需要工艺卡，直接分配检测小组')){
            doAllocate(id)
        }
    }else {
        doAllocate(id)
    }
}
function doAllocate(id) {
    wc_id = id;
    var sentMessage = {
        "requestType": "list",
        "part_id": user.staff_part
    }
    var postData;
    $.ajax({
        url: '/user/team',
        type: 'post',
        data: sentMessage,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
                console.log(data)
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });

    for (var i = 0; i < postData.length; i++) {
        teamName.push([postData[i].team_name, postData[i].team_id, postData[i].team_uid]);
    }
    var teamOption = document.getElementById("groupOption");
    //载入所有检测小组
    for (let i = 0; i < teamName.length; i++) {
        let option = document.createElement("option");
        option.innerHTML = teamName[i][0];
        teamOption.appendChild(option);
    }
    //显示弹窗
    document.getElementById("chooseGroup").setAttribute("class", "modal fade in");
    document.getElementById("chooseGroup").setAttribute("style", "display: block; padding-right: 17px;");
}
function OnCloseGroup(name) {
    //隐藏弹窗
    document.getElementById(name).setAttribute("class", "modal fade");
    document.getElementById(name).setAttribute("style", "display: none;");
}

//确定（分配检测小组）按钮的点击事件
function OnConfirmGroup(name) {
    let index = document.getElementById("groupOption").selectedIndex;
    let date = new Date();
    var sentMessage = {
        "requestType": "manager",
        "tp_team": teamName[index][1],
        "tp_uid": teamName[index][2],
        "tp_start": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
        "tp_wcid": wc_id,
    }
    $.ajax({
        url: '/op/allot',
        type: 'post',
        data: sentMessage,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    OnCloseGroup(name);
    location.reload();//刷新
}